package com.jingdianjichi.subject.application.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectAnswerConvert;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConvert;
import com.jingdianjichi.subject.application.convert.SubjectInfoConvert;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectInfoDTO;
import com.jingdianjichi.subject.common.entity.PageResult;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.common.enums.IsDeleteFlagEnum;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.service.Imp.SubjectInfoServiceDomainImp;
import com.jingdianjichi.subject.domain.service.SubjectDomainService;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectInfo;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectInfoService;
import com.jingdianjichi.subject.infra.basic.service.impl.SubjectInfoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 刷题controller
 */
@Slf4j
@RequestMapping("/subject")
@RestController
public class SubjectController {
    @Resource
    private SubjectInfoServiceImpl subjectInfoService;
    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;
    @Resource
    private SubjectDomainService subjectDomainInfoService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectInfoDTO  dto的日志:{}", subjectInfoDTO);
            }
            //guava 参数校验 对参数进行规则 自定义报错信息
            Preconditions.checkNotNull(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()),"分类id不能为空");
            Preconditions.checkNotNull(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()),"标签id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoConvert.INSTANCE.convertDtoToBo(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOS = SubjectAnswerConvert.INSTANCE.convertDToToBo(subjectInfoDTO.getOptionList());

            subjectInfoBO.setOptionList(subjectAnswerBOS);
            SubjectInfo subjectInfo=SubjectInfoConvert.INSTANCE.convertBoToSubject(subjectInfoBO);
            subjectDomainInfoService.add(subjectInfoBO);
            return Result.ok(true);
        }catch (Exception e) {
            log.error("SubjectCategoryController.add error:{}",e.getMessage());
            return Result.fail("插入失败！！");
        }

    }

    /**
     * 查询题目列表
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectInfoDTO:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"标签id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(),"分类id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoConvert.INSTANCE.convertDtoToBo(subjectInfoDTO);
            PageResult<SubjectInfoBO>  pageResult=subjectDomainInfoService.getSubjectPage(subjectInfoBO);

            return Result.ok(pageResult);
        }catch (Exception e) {
            log.error("SubjectCategoryController.getSubjectPage error:{}",e.getMessage());
        }
        return Result.fail("失败！");
    }

    /**
     * 查询题目详情
     * @param subjectInfoDTO
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectInfoDTO:{}", JSON.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(),"题目id不能为空");
            SubjectInfoBO subjectInfoBO = SubjectInfoConvert.INSTANCE.convertDtoToBo(subjectInfoDTO);
            SubjectInfoBO result=subjectDomainInfoService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO resultDto = SubjectInfoConvert.INSTANCE.SubjectInfoBoToDto(result);
            return Result.ok(resultDto);
        }catch (Exception e) {
            log.error("SubjectCategoryController.querySubjectInfo error:{}",e.getMessage());
        }
        return Result.fail("查询详情失败！");
    }
}
